package io.renren.modules.sys.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.AttendanceEntity;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.AttendanceService;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-24 20:52:24
 */
@RestController
@RequestMapping("sys/attendance")
public class AttendanceController  extends AbstractController {
    @Autowired
    private AttendanceService attendanceService;
    
	@Autowired
	private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
    	SysUserEntity sysUserEntity=getUser();
		if(sysUserEntity.getUserId().intValue()!=Constant.SUPER_ADMIN) {
			params.put("userId", sysUserEntity.getUserId().intValue());
		}
        PageUtils page = attendanceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        AttendanceEntity attendance = attendanceService.selectById(id);

        return R.ok().put("attendance", attendance);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttendanceEntity attendance){
    	SysUserEntity sysUserEntity=getUser();
		SysDeptEntity sysDeptEntity = sysDeptService.selectById(sysUserEntity.getDeptId());
		Date date = new Date();
    	attendance.setAddTime(date);
    	attendance.setUserId(sysUserEntity.getUserId().intValue());
    	attendance.setUserName(sysUserEntity.getUsername());
    	attendance.setDepName(sysDeptEntity.getName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	attendance.setDate(sdf.format(date));
		attendance.setWeek(this.getWeekOfDate(date));    
		if(attendance.getType()==1) {//早上
			if(beforeDate(date,"9:00")) {
				attendance.setStatus(0);
			}else {
				attendance.setStatus(1);
			}
		}else {//晚上
			if(beforeDate(date,"18:00")) {
				attendance.setStatus(1);
			}else {
				attendance.setStatus(0);
			}
		}
		attendanceService.insert(attendance);
        return R.ok();
    }
    

    public  Boolean beforeDate(Date now,String time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date ten = null;
        try {
            ten = format.parse(time);
            now = new Date();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ten.before(now);
    }
    
    public String getWeekOfDate(Date dt) {
    	String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(dt);
    	int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    	if (w < 0)
    		w = 0;
    	 return weekDays[w];
   }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttendanceEntity attendance){
        ValidatorUtils.validateEntity(attendance);
        attendanceService.updateAllColumnById(attendance);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        attendanceService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
